/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase;

import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.uml2.uml.Constraint;

import com.cea.papyrus.core.actions.delete.DeleteFromDiagramAction;
import com.cea.papyrus.core.actions.delete.DeleteFromModelAction;
import com.cea.papyrus.core.editor.EditorContextMenuProvider;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.NodeNameDisplayEditPart;
import com.cea.papyrus.diagram.usecase.editpart.ConstraintEditPart;
import com.cea.papyrus.diagram.usecase.helper.EdgeExtendHelper;

/**
 * Provides context menu actions for the editor.
 */
class UseCaseDiagramContextMenuProvider extends EditorContextMenuProvider {

	/**
	 * Instantiate a new menu context provider for the specified EditPartViewer
	 * and ActionRegistry.
	 * 
	 * @param viewer the editor's graphical viewer
	 * @param modelManager the model manager
	 * @param registry the editor's action registry
	 * 
	 * @throws IllegalArgumentException if registry is <tt>null</tt>.
	 */
	public UseCaseDiagramContextMenuProvider(EditPartViewer viewer, ActionRegistry registry,ModelManager modelManager) {
		super(viewer,registry,modelManager);		
	}

	/**
	 * Overriden to disable DELETE_FROM_MODEL_ACTION and DELETE_FROM_DIAGRAM_ACTION 
	 * for actor name, use case name and extend condition
	 * This is coherent with the restriction applied on the outline.
	 * @param menu 
	 * @see com.cea.papyrus.editor.EditorContextMenuProvider#buildContextMenu(IMenuManager)
	 */
	@Override
	public void buildContextMenu(IMenuManager menu) {
		super.buildContextMenu(menu);
		
		//additional restrictions
		//on actor and usecase name
		if(!getViewer().getSelectedEditParts().isEmpty() && 
				(getViewer().getSelectedEditParts().get(0) instanceof NodeNameDisplayEditPart)){
			menu.remove(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_ID);
			menu.remove(DeleteFromDiagramAction.DELETE_FROM_DIAGRAM_ACTION_ID);
		}
		
		//on extend conditions
		if(!getViewer().getSelectedEditParts().isEmpty() && 
				(getViewer().getSelectedEditParts().get(0) instanceof ConstraintEditPart)){
			Constraint contraint = (Constraint)((ConstraintEditPart)getViewer().getSelectedEditParts().get(0)).getUmlElement();		
			if (contraint.getName().equals(EdgeExtendHelper.EXTEND_CONDITION_NAME)){
				menu.remove(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_ID);
			}
		}	
	}
}